import {
  Column,
  Entity,
  JoinTable,
  ManyToMany,
  PrimaryGeneratedColumn,
  OneToMany,
} from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { CommonEntity } from 'src/common/entity/index.entity';
import { PermissionEntity } from '../../permission/entities/permission.entity';
import { UserEntity } from '../../user/entities/user.entity';

@Entity('role', {})
export class RoleEntity extends CommonEntity {
  @ApiProperty({ default: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' })
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ApiProperty({ default: '管理员' })
  @Column('varchar', { name: 'role_name', length: 50, comment: '角色名' })
  roleName: string;

  @OneToMany(() => UserEntity, (users) => users.role)
  users: UserEntity[];

  @ApiProperty({ type: PermissionEntity, isArray: true })
  @ManyToMany(() => PermissionEntity)
  @JoinTable({
    name: 'role_permission_relation',
    joinColumn: {
      name: 'role_id',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'permission_id',
      referencedColumnName: 'id',
    },
  })
  permissions: PermissionEntity[];
}
